<!--
 * @Description:
 * @Author: william
 * @Date: 2025-02-17 16:47:40
 * @LastEditors: william
 * @LastEditTime: 2025-04-11 16:32:20
-->
<template>
  <BaseDialog
    v-model="dialogVisible"
    :title="g_t('public.create')"
    width="850px"
    :before-close="handleClose"
  >
    <el-form
      ref="refDialogForm"
      v-loading="formLoading"
      class="set-dialog-form"
      :label-width="language === 'en' ? '180px' : '100px'"
      :model="formData"
      :rules="rules"
    >
      <el-row>
        <el-divider>
          <div class="col-title">{{ $t('userCenter.orgInfo') }}</div>
        </el-divider>
        <el-col :span="12">
          <el-form-item :label="$t('userCenter.orgName')" prop="tname">
            <TextInput
              v-model="formData.tname"
              :label="$t('userCenter.orgName')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('userCenter.orgId')" prop="identifier">
            <TextInput
              v-model="formData.identifier"
              :label="$t('userCenter.orgId')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('public.remark')" prop="desc">
            <TextInput
              v-model="formData.desc"
              type="textarea"
              :rows="2"
              :label="$t('public.remark')"
            />
          </el-form-item>
        </el-col>
        <el-divider>
          <div class="col-title">{{ $t('userCenter.adminInfo') }}</div>
        </el-divider>
        <el-col :span="12">
          <el-form-item :label="$t('public.userName')" prop="userName">
            <InputUserName v-model="formData.userName" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('public.nickName')" prop="nickName">
            <TextInput
              v-model="formData.nickName"
              :label="$t('public.nickName')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('public.password')" prop="password">
            <TextInput
              v-model="formData.password"
              :label="$t('public.password')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            :label="$t('public.confirmPassword')"
            prop="confirmPassword"
          >
            <TextInput
              v-model="formData.confirmPassword"
              :label="$t('public.confirmPassword')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('public.phone')" prop="phone">
            <InputPhone v-model="formData.phone" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('public.email')" prop="email">
            <TextInput v-model="formData.email" :label="$t('public.email')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('userCenter.role')" prop="roleIdList">
            <SelectRole v-model="formData.roleIdList" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="form-dialog-footer">
        <el-button plain @click="handleClose">{{
          $t('public.cancel')
        }}</el-button>
        <el-button type="primary" @click="handleSubmitForm">{{
          $t('public.confirm')
        }}</el-button>
      </div>
    </template>
  </BaseDialog>
</template>

<script setup>
defineProps({
  id: {
    type: String,
    default: ''
  }
})

const emit = defineEmits(['update'])
const dialogVisible = defineModel({
  type: Boolean,
  default: false
})
const { language } = storeToRefs(useAppStore())

const refDialogForm = ref(null)
const formLoading = ref(false)
const formData = ref({
  tname: '',
  identifier: '',
  desc: '',
  userName: '',
  nickName: '',
  password: '',
  confirmPassword: '',
  phone: '',
  email: '',
  roleIdList: []
})
const rules = {
  tname: g_rules.commonInputText(g_t('userCenter.orgName')),
  identifier: g_rules.commonInputText(g_t('userCenter.orgId')),
  desc: g_rules.commonInputText(g_t('public.remark'), false),
  userName: g_rules.userName,
  nickName: g_rules.commonInputText(g_t('public.nickName')),
  password: g_rules.password,
  confirmPassword: g_rules.confirmPassword(formData.value, 'password'),
  phone: g_rules.phone({
    required: false
  }),
  email: g_rules.email({
    required: false
  })
  // roleIdList: g_rules.selectRule(g_t('userCenter.role'))
}

onMounted(() => {})

const handleClose = () => {
  dialogVisible.value = false
}

const handleSubmitForm = () => {
  if (formLoading.value) return
  refDialogForm.value.validate(valid => {
    // console.log('handleSubmitForm', formData.value)
    if (!valid) return
    formLoading.value = true
    g_api.userCenter
      .apiTenantCreate(formData.value)
      .then(res => {
        if (res.code === '200') {
          ElMessage({
            message: res.message,
            type: 'success'
          })
          handleClose()
          emit('update')
        }
      })
      .finally(() => {
        formLoading.value = false
      })
  })
}
</script>

<style scoped lang="scss"></style>
